package com.imoocchapterone;

import android.support.annotation.Nullable;

import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;

/**
 * @Title:ImageSelectorModule
 * @Package:com.imoocchapterone
 * @Description:实现对react-native的接口暴露
 * @Auther:YJH
 * @Email:yuannunhua@gmail.com
 * @Date:2018/6/1415:59
 */
public class ImageSelectorModule extends ReactContextBaseJavaModule implements ImageSelector {

    private ImageSelectorimpl mImageSelectorimpl;
    private ReactApplicationContext reactContext;

    public ImageSelectorModule(ReactApplicationContext reactContext) {
        super(reactContext);
        //这里做实现发射器事件基础配置
        this.reactContext=reactContext;
    }


    @Override
    public String getName() {
        return "ImageSelector";
    }


    /**
     * 功能：向React Native暴露的接口，并执行数据交互操作
     */
    @ReactMethod
    @Override
    public void chooseImgCallback(Callback successCallback, Callback errorCallback) {
        if(null == mImageSelectorimpl){
            mImageSelectorimpl = ImageSelectorimpl.of(getCurrentActivity());
            getReactApplicationContext().addActivityEventListener(mImageSelectorimpl);
        }
        mImageSelectorimpl.chooseImgCallback(successCallback, errorCallback);
    }

    @ReactMethod
    @Override
    public void chooseImgPromise(Promise promise) {
        if(null == mImageSelectorimpl){
            mImageSelectorimpl = ImageSelectorimpl.of(getCurrentActivity());
            getReactApplicationContext().addActivityEventListener(mImageSelectorimpl);
        }

//        mImageSelectorimpl.updateActivity(getCurrentActivity());
        mImageSelectorimpl.chooseImgPromise(promise);
    }

    /**
     * 功能：发射器实现通知功能方法
     * @param eventName
     * @param params
     */
    @Override
    public void sendEventMethod(String eventName, @Nullable WritableMap params) {
        this.sendEventMethod(reactContext, eventName, params);
    }

    @Override
    public void sendEventMethod(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
        mImageSelectorimpl.sendEventMethod(reactContext, eventName, params);
    }


}
